package com.bwhx.util;

import android.bluetooth.BluetoothDevice;

public class ETapeDecrypt {


    public static boolean connectechi = true;

    public static BluetoothDevice device;

    public ETapeDecrypt() {

    }

    public static long Decrypt(byte[] Indata) {
        int Lcheck = 0;
        byte Ocheck = 0;
        int Length = 0;
        if ((Indata[3] & 128) != 0) {
            Length |= 4;
        }

        if ((Indata[3] & 64) != 0) {
            Length |= 128;
        }

        if ((Indata[3] & 32) != 0) {
            Ocheck = (byte)(Ocheck | 68);
        }

        if ((Indata[3] & 16) != 0) {
            Length |= 1;
        }

        if ((Indata[3] & 8) != 0) {
            Length |= 64;
        }

        if ((Indata[3] & 4) != 0) {
            Lcheck |= 17476;
        }

        if ((Indata[3] & 2) != 0) {
            Length |= 4096;
        }

        if ((Indata[3] & 1) != 0) {
            Length |= 512;
        }

        if ((Indata[4] & 128) != 0) {
            Length |= 16384;
        }

        if ((Indata[4] & 64) != 0) {
            Length |= 2048;
        }

        if ((Indata[4] & 32) != 0) {
            Ocheck = (byte)(Ocheck | 17);
        }

        if ((Indata[4] & 16) != 0) {
            Ocheck = (byte)(Ocheck | 136);
        }

        if ((Indata[4] & 8) != 0) {
            Length |= 16;
        }

        if ((Indata[4] & 4) != 0) {
            Length |= 32768;
        }

        if ((Indata[4] & 2) != 0) {
            Lcheck |= 4369;
        }

        if ((Indata[4] & 1) != 0) {
            Lcheck |= 34952;
        }

        if ((Indata[1] & 128) != 0) {
            Length |= 256;
        }

        if ((Indata[1] & 64) != 0) {
            Ocheck = (byte)(Ocheck | 34);
        }

        if ((Indata[1] & 32) != 0) {
            Length |= 32;
        }

        if ((Indata[1] & 16) != 0) {
            Lcheck |= 8738;
        }

        if ((Indata[1] & 8) != 0) {
            Length |= 8192;
        }

        if ((Indata[1] & 4) != 0) {
            Length |= 2;
        }

        if ((Indata[1] & 2) != 0) {
            Length |= 1024;
        }

        if ((Indata[1] & 1) != 0) {
            Length |= 8;
        }



        Length ^= Lcheck;
        Ocheck = (byte)(Ocheck ^ 76);
        Indata[2] ^= Ocheck;
        Indata[5] ^= Ocheck;
        int Offset;
        if (Indata[5] < 0) {
            Offset = Indata[5] + 256;
        } else {
            Offset = Indata[5];
        }

        Offset <<= 8;
        if (Indata[2] < 0) {
            Offset = Offset + Indata[2] + 256;
        } else {
            Offset += Indata[2];
        }

        Offset -= 13845;
        Length += Offset;

        return (long)Length;
    }

    /**

     * byte数组转换成16进制字符数组

     * @param src

     * @return

     */

    public static String[] bytesToHexStrings(byte[] src){

        if (src == null || src.length <= 0) {

            return null;

        }

        String[] str = new String[src.length];



        for (int i = 0; i < src.length; i++) {

            int v = src[i] & 0xFF;

            String hv = Integer.toHexString(v);

            if (hv.length() < 2) {

                str[i] = "0";

            }

            str[i] = hv;

        }

        return str;

    }
}
